package com.ef.web.action;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.ServletActionContext;

import com.ef.constant.StatusType;
import com.ef.constant.SystemContant;
import com.ef.domain.MarketingPolicy;
import com.ef.domain.School;
import com.ef.domain.User;
import com.ef.web.domain.CCPerformance;
import com.ef.web.domain.CRPerformance;
import com.ef.web.domain.Financial;
import com.ef.web.domain.RevenueCount;
import com.ef.web.domain.TMPerformance;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public abstract class ReportAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(BaseAction.class);
	protected static final String LIST = "list";

	public String list() {

		return LIST;
	}

	public String export() {
		return NONE;
	}

	public  void exportExcel(HttpServletResponse response,
			Map<String, Object> map) {
		response.setContentType("application/ms-excel;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename="
					+ new String(String.valueOf(map.get("doc.filename"))
							.getBytes("gb2312"), "iso8859-1") + ".xls");
			createRevenueExcelDocument(map, response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public  void createRevenueExcelDocument(Map<String, Object> map,
			ServletOutputStream out)  {
		File file = new File(ServletActionContext
					.getServletContext().getRealPath("/temp"));
		if(!file.exists()){
			file.mkdir();
		}
		WritableWorkbook workBook;
		try {
			workBook = Workbook.createWorkbook(new File(ServletActionContext
					.getServletContext().getRealPath("/temp/export.xls")));
			workBook.setProtected(false);
			WritableSheet wst = workBook.createSheet("export", 0);

			List<RevenueCount> list = (List<RevenueCount>) map.get("doc.data");
			String title = (String) map.get("doc.title");
			String New = (String) map.get("doc.new");
			String Old = (String) map.get("doc.old");

			wst.addCell(new Label(0, 0, title));
			wst.addCell(new Label(1, 0, New));
			wst.addCell(new Label(2, 0, Old));

			for (int i = 0; i < list.size(); i++) {
				wst.addCell(new Label(0, i + 1, list.get(i).getTitle()));
				wst.addCell(new Label(1, i + 1, list.get(i).getCurrentSum()
						.toString()));
				wst.addCell(new Label(2, i + 1, list.get(i).getLastSum()
						.toString()));
			}

			workBook.write();
			workBook.close();
			RequestDispatcher view = getRequest().getRequestDispatcher(
					 "../../temp/export.xls");
			try {
				view.forward(getRequest(), getResponse());
			} catch (ServletException e) {
				 
				e.printStackTrace();
			}

		} catch (IOException e) {

			e.printStackTrace();
		} catch (RowsExceededException e) {

			e.printStackTrace();
		} catch (WriteException e) {

			e.printStackTrace();
		}

	}
	public  void exportExcel2(HttpServletResponse response,
			Map<String, Object> map) {
		response.setContentType("application/ms-excel;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename="
					+ new String(String.valueOf(map.get("doc.filename"))
							.getBytes("gb2312"), "iso8859-1") + ".xls");
			createCCPerformanceExcelDocument(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * CC Performance
	 * @param map
	 */
	private void createCCPerformanceExcelDocument(Map<String,Object> map){
		File file = new File(ServletActionContext
				.getServletContext().getRealPath("/temp"));
	if(!file.exists()){
		file.mkdir();
	}
	WritableWorkbook workBook;
	try {
		workBook = Workbook.createWorkbook(new File(ServletActionContext
				.getServletContext().getRealPath("/temp/export.xls")));
		workBook.setProtected(false);
		WritableSheet wst = workBook.createSheet("export", 0);

		List<CCPerformance> list = (List<CCPerformance>) map.get("doc.data");
		String firstLine = (String) map.get("doc.line");
		wst.addCell(new Label(0,0,firstLine));

		wst.addCell(new Label(0, 1, "CC"));
		wst.addCell(new Label(1, 1, "Revenue"));
		wst.addCell(new Label(2, 1, "Call"));
		wst.addCell(new Label(3, 1, "Contract"));
		wst.addCell(new Label(4, 1, "Closing Rate"));

		for (int i = 0; i < list.size(); i++) {
			wst.addCell(new Label(0, i + 2, list.get(i).getTitle()));
			wst.addCell(new Label(1, i + 2, list.get(i).getRevenue()
					.toString()));
			wst.addCell(new Label(2, i + 2, String.valueOf(list.get(i).getCall()) ));
			wst.addCell(new Label(3, i + 2, String.valueOf(list.get(i).getContract()) ));
			wst.addCell(new Label(4, i + 2, String.valueOf(list.get(i).getRate())+"%" ));
		}

		workBook.write();
		workBook.close();
		RequestDispatcher view = getRequest().getRequestDispatcher(
				 "../../temp/export.xls");
		try {
			view.forward(getRequest(), getResponse());
		} catch (ServletException e) {
			 
			e.printStackTrace();
		}

	} catch (IOException e) {

		e.printStackTrace();
	} catch (RowsExceededException e) {

		e.printStackTrace();
	} catch (WriteException e) {

		e.printStackTrace();
	}

	}
	
	public  void exportExcel3(HttpServletResponse response,
			Map<String, Object> map) {
		response.setContentType("application/ms-excel;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename="
					+ new String(String.valueOf(map.get("doc.filename"))
							.getBytes("gb2312"), "iso8859-1") + ".xls");
			createCRPerformanceExcelDocument(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * CR Performance
	 * @param map
	 */
	private void createCRPerformanceExcelDocument(Map<String,Object> map){
		File file = new File(ServletActionContext
				.getServletContext().getRealPath("/temp"));
	if(!file.exists()){
		file.mkdir();
	}
	WritableWorkbook workBook;
	try {
		workBook = Workbook.createWorkbook(new File(ServletActionContext
				.getServletContext().getRealPath("/temp/export.xls")));
		workBook.setProtected(false);
		WritableSheet wst = workBook.createSheet("export", 0);

		List<CRPerformance> list = (List<CRPerformance>) map.get("doc.data");
		String firstLine = (String) map.get("doc.line");
		wst.addCell(new Label(0,0,firstLine));

		wst.addCell(new Label(0, 1, "CR"));
		wst.addCell(new Label(1, 1, "Call"));
		wst.addCell(new Label(2, 1, "ShowUp"));

		for (int i = 0; i < list.size(); i++) {
			wst.addCell(new Label(0, i + 2, list.get(i).getTitle()));
			wst.addCell(new Label(1, i + 2, String.valueOf(list.get(i).getCall()) ));
			wst.addCell(new Label(2, i + 2, String.valueOf(list.get(i).getShowup()) ));
		}

		workBook.write();
		workBook.close();
		RequestDispatcher view = getRequest().getRequestDispatcher(
				 "../../temp/export.xls");
		try {
			view.forward(getRequest(), getResponse());
		} catch (ServletException e) {
			 
			e.printStackTrace();
		}

	} catch (IOException e) {

		e.printStackTrace();
	} catch (RowsExceededException e) {

		e.printStackTrace();
	} catch (WriteException e) {

		e.printStackTrace();
	}
	}
	
	public  void exportExcel4(HttpServletResponse response,
			Map<String, Object> map) {
		response.setContentType("application/ms-excel;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename="
					+ new String(String.valueOf(map.get("doc.filename"))
							.getBytes("gb2312"), "iso8859-1") + ".xls");
			createTMPerformanceExcelDocument(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * TM Performance
	 * @param map
	 */
	private void createTMPerformanceExcelDocument(Map<String,Object> map){
		File file = new File(ServletActionContext
				.getServletContext().getRealPath("/temp"));
	if(!file.exists()){
		file.mkdir();
	}
	WritableWorkbook workBook;
	try {
		workBook = Workbook.createWorkbook(new File(ServletActionContext
				.getServletContext().getRealPath("/temp/export.xls")));
		workBook.setProtected(false);
		WritableSheet wst = workBook.createSheet("export", 0);

		List<TMPerformance> list = (List<TMPerformance>) map.get("doc.data");
		String firstLine = (String) map.get("doc.line");
		wst.addCell(new Label(0,0,firstLine));

		wst.addCell(new Label(0, 1, "TM"));
		wst.addCell(new Label(1, 1, "Call"));
		wst.addCell(new Label(2, 1, "SetAppt"));
		wst.addCell(new Label(3, 1, "UnCall"));
		wst.addCell(new Label(4, 1, "ShowUp"));

		for (int i = 0; i < list.size(); i++) {
			wst.addCell(new Label(0, i + 2, list.get(i).getTitle()));
			wst.addCell(new Label(1, i + 2, String.valueOf(list.get(i).getCall()) ));
			wst.addCell(new Label(2, i + 2, String.valueOf(list.get(i).getSetappt()) ));
			wst.addCell(new Label(3, i + 2, String.valueOf(list.get(i).getUncall()) ));
			wst.addCell(new Label(4, i + 2, String.valueOf(list.get(i).getShowup()) ));
		}

		workBook.write();
		workBook.close();
		RequestDispatcher view = getRequest().getRequestDispatcher(
				 "../../temp/export.xls");
		try {
			view.forward(getRequest(), getResponse());
		} catch (ServletException e) {
			 
			e.printStackTrace();
		}

	} catch (IOException e) {

		e.printStackTrace();
	} catch (RowsExceededException e) {

		e.printStackTrace();
	} catch (WriteException e) {

		e.printStackTrace();
	}
	}
	
	public  void exportExcel5(HttpServletResponse response,
			Map<String, Object> map) {
		response.setContentType("application/ms-excel;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename="
					+ new String(String.valueOf(map.get("doc.filename"))
							.getBytes("gb2312"), "iso8859-1") + ".xls");
			createFinancialExcelDocument(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Financial
	 * @param map
	 */
	private void createFinancialExcelDocument(Map<String,Object> map){
		File file = new File(ServletActionContext
				.getServletContext().getRealPath("/temp"));
	if(!file.exists()){
		file.mkdir();
	}
	WritableWorkbook workBook;
	try {
		workBook = Workbook.createWorkbook(new File(ServletActionContext
				.getServletContext().getRealPath("/temp/export.xls")));
		workBook.setProtected(false);
		WritableSheet wst = workBook.createSheet("export", 0);

		List<Financial> list = (List<Financial>) map.get("doc.data");
		String firstLine = (String) map.get("doc.line");
		wst.addCell(new Label(0,0,firstLine));

		wst.addCell(new Label(0, 1, "Month"));
		wst.addCell(new Label(1, 1, "Paid"));
		wst.addCell(new Label(2, 1, "Refund"));
		wst.addCell(new Label(3, 1, "Sum"));
		wst.addCell(new Label(4, 1, "Contract(valid)"));
		wst.addCell(new Label(5, 1, "Contract(invalid)"));

		for (int i = 0; i < list.size(); i++) {
			wst.addCell(new Label(0, i + 2, list.get(i).getMonth()));
			wst.addCell(new Label(1, i + 2, String.valueOf(list.get(i).getPaid()) ));
			wst.addCell(new Label(2, i + 2, String.valueOf(list.get(i).getRefund()) ));
			wst.addCell(new Label(3, i + 2, String.valueOf(list.get(i).getSum()) ));
			wst.addCell(new Label(4, i + 2, String.valueOf(list.get(i).getValid()) ));
			wst.addCell(new Label(5, i + 2, String.valueOf(list.get(i).getInvalid()) ));
		}

		workBook.write();
		workBook.close();
		RequestDispatcher view = getRequest().getRequestDispatcher(
				 "../../temp/export.xls");
		try {
			view.forward(getRequest(), getResponse());
		} catch (ServletException e) {
			 
			e.printStackTrace();
		}

	} catch (IOException e) {

		e.printStackTrace();
	} catch (RowsExceededException e) {

		e.printStackTrace();
	} catch (WriteException e) {

		e.printStackTrace();
	}
	}
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	protected User getSessionUser() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		User user = (User) session.get(SystemContant.CURRENT_USER);
        if (user==null){
        	try {
        		String path = getRequest().getContextPath();
        		String basePath = getRequest().getScheme() + "://"
    			+ getRequest().getServerName() + ":" + getRequest().getServerPort()
    			+ path + "/";
				ServletActionContext.getResponse().sendRedirect(basePath+"login.jsp");
			} catch (IOException e) {
				 log.error("getSessionUser Error:"+e);
			}
        }
		return user;
	}
	


}
